package com.jc.service.menu;

import com.alibaba.fastjson.JSONObject;
import com.jc.common.util.CheckNullUtils;
import com.jc.common.util.WebUtils;
import com.jc.ds.base.service.BaseServiceException;
import com.jc.ds.mapper.RolePermissionMapper;
import com.jc.ds.model.*;
import com.jc.service.permission.PermissionService;
import com.jc.service.role.RoleService;
import com.jc.service.rolepermission.RolePermissionService;
import com.jc.service.user.UserService;
import com.jc.service.userrole.UserRoleService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.UnsupportedEncodingException;
import java.util.*;

@SuppressWarnings("all")
@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private PermissionService permissionService;

    @Autowired
    private RolePermissionService rolePermissionService;


    @Autowired
    private UserService userService;

    @Autowired
    private UserRoleService userRoleService;

    @Autowired
    private RoleService roleService;


    /**
     * 所有菜单
     */
    @Override
    public List<Map<String, Object>> queryMenuList() {
        //查询所有父级菜单
        List<Permission> permissions = permissionService.queryAll(new Permission() {{
            setParentId(0L);
        }});
        List<Map<String, Object>> resultMap = new ArrayList<>();
        for (Permission permission : permissions) {
            Map<String, Object> permissionMap = WebUtils.beanToMap(permission);
            List<Permission> permissionsList = permissionService.queryAll(new Permission() {{
                setParentId(permission.getId());
                setPermissionType(1);
            }});
            ArrayList<Map<String, Object>> objects = new ArrayList<>();
            for (Permission permission1 : permissionsList) {
                List<Permission> permissions1 = this.permissionService.queryAll(new Permission() {{
                    setParentId(permission1.getId());
                    setPermissionType(1);
                }});
                Map<String, Object> map = WebUtils.beanToMap(permission1);
                map.put("threeMenus", permissions1);
                objects.add(map);

            }
            permissionMap.put("twoMenus", objects);
            resultMap.add(permissionMap);
        }
        return resultMap;
    }


    /**
     * 菜单权限
     */
    @Override
    public List<Permission> menuListShiro(Long userId) {
        Map<String, Object> result = new HashMap<>();
        ArrayList<Map<String, Object>> objects = null;
        ArrayList<Map<String, Object>> objects1 = null;
        Set<Long> perIds = new HashSet<>();

        //查询当前用户是否存在
        List<User> users = userService.queryAll(new User() {{
            setId(userId);
        }});
        if (CollectionUtils.isEmpty(users)) throw new BaseServiceException("当前用户不存在!");
        // 查询用户的角色
        User user = users.get(0);
        List<UserRole> userRoles = userRoleService.queryAll(new UserRole() {{
            setUserId(user.getId());
        }});
        List<Long> roleIds = new ArrayList<>();
        for (UserRole userRole : userRoles) {
            roleIds.add(userRole.getRoleId());
        }
        //查询所有权限
        for (Long roleId : roleIds) {
            List<RolePermission> rolePermissions = rolePermissionService.queryAll(new RolePermission() {{
                setRoleId(roleId);
            }});
            for (RolePermission rolePermission : rolePermissions) {
                perIds.add(rolePermission.getPermissionId());
            }
        }
        //查询所有的菜单
        List<Permission> permissions = permissionService.queryAll(new Permission() {{
            setPermissionType(1);
        }});
        List<Permission> menus = new ArrayList<>();

        for (Long perId : perIds) {
            for (Permission permission : permissions) {
                if (permission.getId().toString().equals(perId.toString())) {
                    menus.add(permission);
                }
            }
        }
        List<Permission> rootMenu = new ArrayList<Permission>();
        for (Permission nav : menus) {
            if (nav.getParentId().toString().equals("0")) {//父节点是0的，为根节点。
                rootMenu.add(nav);
            }
        }
        /* 根据Menu类的order排序 */
        Collections.sort(rootMenu, order());
        //为根菜单设置子菜单，getClild是递归调用的
        for (Permission nav : rootMenu) {
            /* 获取根节点下的所有子节点 使用getChild方法*/
            Map<String, List<Permission>> child = getChild(nav.getId(), menus);
            nav.setChildren(child.get("menuList"));//给根节点设置子节点
        }

        return rootMenu;
    }


    /**
     * 获取子节点
     *
     * @param id      父节点id
     * @param allMenu 所有菜单列表
     * @return 每个根节点下，所有子菜单列表
     */
    public Map<String, List<Permission>> getChild(Long id, List<Permission> allMenu) {
        //子菜单
        List<Permission> childList = new ArrayList<Permission>();
        for (Permission nav : allMenu) {
            // 遍历所有节点，将所有菜单的父id与传过来的根节点的id比较
            //相等说明：为该根节点的子节点。
            if (nav.getParentId().equals(id)) {
                childList.add(nav);
            }
        }
        //递归
        for (Permission nav : childList) {
            nav.setChildren(getChild(nav.getId(), allMenu).get("menuList"));
        }
        Collections.sort(childList, order());//排序
        //如果节点下没有子节点，返回一个空List（递归退出）
        if (childList.size() == 0 && childList.size() == 0) {
            return new HashMap<>();
        }
        return new HashMap<String, List<Permission>>() {{
            put("menuList", childList);
        }};
    }


    /*
     * 排序,根据order排序
     */
    public Comparator<Permission> order() {
        Comparator<Permission> comparator = new Comparator<Permission>() {
            @Override
            public int compare(Permission o1, Permission o2) {
                if (o1.getId() != o2.getId()) {
                    return (int) (o1.getId() - o2.getId());
                }
                return 0;
            }
        };
        return comparator;
    }


//        String param1 = String.valueOf(param.get("param"));
//
//        Map<String, Object> jsonObject = JSONObject.parseObject(param1);
//        List<User> users = this.userService.queryAll(new User() {{
//            setName(String.valueOf(jsonObject.get("name")));
//        }});
//        List<UserRole> userRoles = this.userRoleService.queryAll(new UserRole() {{
//            setUserId(users.get(0).getId());
//        }});
//        for (UserRole userRole : userRoles) {
//            List<RolePermission> rolePermissions = this.rolePermissionMapper.selectAll(new RolePermission() {{
//                setRoleId(userRole.getRoleId());
//            }});
//            for (RolePermission rolePermission : rolePermissions) {
//                List<Permission> permissions = this.permissionService.queryAll(new Permission() {{
//                    setPermissionType(1);
//                    setId(rolePermission.getPermissionId());
//                }});
//                for (Permission permission : permissions) {
//                    threeMenuIdList.add(String.valueOf(permission.getId()));
//                }
//
//            }
//        }
//        //查出所有
//        //所有的父级
//        List<Permission> permissions = this.permissionService.queryAll(new Permission() {{
//            setPermissionType(1);
//            setParentId(Long.valueOf("0"));
//        }});
//
//        //所有菜单
//        List<Permission> permissionss = this.permissionService.queryAll(new Permission() {{
//            setPermissionType(1);
//        }});
//        objects = new ArrayList<>();
//
//        for (Permission permissionOut : permissions) {
//            ArrayList<Permission> two = new ArrayList<>();
//            Map<String, Object> permissionOutMap = WebUtils.beanToMap(permissionOut);
//            for (Permission permissionIn : permissionss) {
//                if (permissionOut.getId().equals(permissionIn.getParentId())) {
//                    //就是这次外次的子
//                    two.add(permissionIn);
//                }
//            }
//            ArrayList<Map<String, Object>> twoTwo = new ArrayList<>();
//            //循环2找出3
//            for (Permission out : two) {
//                Map<String, Object> twoMap = WebUtils.beanToMap(out);
//                ArrayList<Permission> three = new ArrayList<>();
//                for (Permission in : permissionss) {
//                    if (out.getId().equals(in.getParentId())) {
//                        //就这二的子
//                        three.add(in);
//                    }
//                }
//                twoMap.put("threeMenus", three);
//                twoTwo.add(twoMap);
//            }
//            permissionOutMap.put("twoMenus", twoTwo);
//            objects.add(permissionOutMap);
//        }
//        objects1 = new ArrayList<>();
//        //用户的三级菜单id
//        for (Map<String, Object> object : objects) {
//            Map<String, Object> ddd = object;
//            ArrayList<Object> twoF = new ArrayList<>();
//            List<Map<String, Object>> twoMapList = (List<Map<String, Object>>) JSONObject.parse(JSONObject.toJSONString(object.get("twoMenus")));
//            for (Map<String, Object> stringObjectMap : twoMapList) {
//                Map<String, Object> fff = stringObjectMap;
//                ArrayList<Object> threeF = new ArrayList<>();
//                List<Map<String, Object>> threeMapList = (List<Map<String, Object>>) JSONObject.parse(JSONObject.toJSONString(stringObjectMap.get("threeMenus")));
//                for (Map<String, Object> threeMap : threeMapList) {
//                    for (String s : threeMenuIdList) {
//                        if (String.valueOf(threeMap.get("id")).equals(s)) {//如果三级菜单相等
//                            threeF.add(threeMap);
//                        }
//                    }
//                }
//                fff.put("threeMenus", threeF);
//                twoF.add(fff);
//            }
//            ddd.put("twoMenus", twoF);
//            objects1.add(ddd);
//        }
//        for (Map<String, Object> m : objects1) {
//            List<Map<String, Object>> twoMapList = (List<Map<String, Object>>) JSONObject.parse(JSONObject.toJSONString(m.get("twoMenus")));
//            for (int i = 0; i < twoMapList.size(); i++) {
//                List<Map<String, Object>> saf = (List<Map<String, Object>>) JSONObject.parse(JSONObject.toJSONString(twoMapList.get(i).get("threeMenus")));
//                if (saf.size() == 0) {
//                    twoMapList.remove(i);
//                    i--;
//                }
//            }
//            m.put("twoMenus", twoMapList);
//
//        }
//        for (int i = 0; i < objects1.size(); i++) {
//            List<Map<String, Object>> twoMapList = (List<Map<String, Object>>) JSONObject.parse(JSONObject.toJSONString(objects1.get(i).get("twoMenus")));
//            if (twoMapList.size() == 0) {
//                objects1.remove(i);
//                i--;
//            }
//        }
//        long end = System.currentTimeMillis();
//        long time = end - start;
//        System.out.println("该方法所化时间为time = " + time);
//
//        return objects1;
//
//    }


    /**
     * 父查子菜单
     */
    @Override
    public Map<String, Object> queryChildMenu(String fatherName) {
        Map<String, Object> result = new HashMap<>();

        List<Permission> permissions = this.permissionService.queryAll(new Permission() {{
            setDescription(fatherName);
            setParentId(Long.valueOf("0"));
        }});
        Long id = permissions.get(0).getId();

        List<Permission> permissionsChild = this.permissionService.queryAll(new Permission() {{
            setParentId(id);
        }});
        ArrayList<Object> objects = new ArrayList<>();

        for (Permission permission : permissionsChild) {
            Map<String, Object> stringObjectHashMap = new HashMap<>();
            stringObjectHashMap.put("description", permission.getDescription());
            stringObjectHashMap.put("id", permission.getId());
            stringObjectHashMap.put("parentId", permission.getParentId());
            stringObjectHashMap.put("url", permission.getUrl());
            if (permission.getIcon() == null) {
                stringObjectHashMap.put("icon", "");
            } else {
                stringObjectHashMap.put("icon", permission.getIcon());
            }
            if (permission.getIcon() == null) {
                stringObjectHashMap.put("token", "");
            } else {
                stringObjectHashMap.put("code", permission.getCode());
            }
            List<Permission> permissions1 = this.permissionService.queryAll(new Permission() {{
                setId(permission.getParentId());
            }});
            String descriptionF = permissions1.get(0).getDescription();
            Long idF = permissions1.get(0).getId();
            stringObjectHashMap.put("descriptionF", descriptionF);
            stringObjectHashMap.put("idF ", idF);
            objects.add(stringObjectHashMap);

        }
        result.put("code", 10000);
        result.put("msg", "success");
        result.put("data", objects);
        return result;
    }

    /**
     * 更换主菜单
     */
    @Override
    public Map<String, Object> changFMenu(String saveFName, String saveZId, Map<String, Object> saveF) {

        Map<String, Object> result = new HashMap<>();


        List<Permission> permissions = this.permissionService.queryAll(new Permission() {{
            setParentId(Long.valueOf("0"));
            setDescription(saveFName);
        }});
        Permission permission = permissions.get(0);

        int modify = this.permissionService.modify(new Permission() {{
            setParentId(permission.getId());
            setId(Long.valueOf(saveZId));
            if (!CheckNullUtils.isNull(String.valueOf(saveF.get("menuSUrlS")))) {
                String menuSUrlS = String.valueOf(saveF.get("menuSUrlS"));
                setUrl(menuSUrlS);

            }
            if (!CheckNullUtils.isNull(String.valueOf(saveF.get("thisMenuS")))) {
                String thisMenuS = String.valueOf(saveF.get("thisMenuS"));
                setDescription(thisMenuS);
            }
        }}, false);
        if (modify == 1) {
            result.put("code", 10000);
            result.put("data", "success");
            result.put("msg", "修改成功");
            return result;
        }
        result.put("code", 6666);
        result.put("data", "fail");
        result.put("msg", "修改失败");
        return result;
    }

    /**
     * 保存父级菜单
     */
    @Override
    public Map<String, Object> saveFatherMenu(Map<String, Object> param) {


        Map<String, Object> result = new HashMap<>();
        int add = 0;
        if (CheckNullUtils.isNull(String.valueOf(param.get("menuId")))) {
            result.put("code", 6666);
            result.put("data", "fail");
            result.put("msg", "没有接收到menuId");
            return result;
        }
        String menuId = String.valueOf(param.get("menuId"));
        if (String.valueOf(param.get("menuId")).equals("1")) {

            if (CheckNullUtils.isNull(String.valueOf(param.get("firstFirstMenu")))) {
                result.put("code", 6666);
                result.put("data", "fail");
                result.put("msg", "没有接收到firstFirstMenu");
                return result;
            }
            String menuF = String.valueOf(param.get("firstFirstMenu"));
            String editOneId = String.valueOf(param.get("editOneId"));

            if (String.valueOf(param.get("idEdit")).equals("yes")) {
                int modify1 = this.permissionService.modify(new Permission() {{
                    setParentId(Long.valueOf("0"));
                    setId(Long.valueOf(editOneId));
                    setDescription(menuF);
                }}, false);


                if (modify1 != 0) {
                    result.put("code", 10000);
                    result.put("data", "更新成功1");
                    result.put("msg", "更新成功1~");
                    return result;
                } else {
                    result.put("code", 6666);
                    result.put("data", "更新失败");
                    result.put("msg", "更新失败~");
                    return result;
                }
            }


            add = this.permissionService.add(new Permission() {{
                setParentId(Long.valueOf("0"));
                setDescription(menuF);
            }}, false);

        }

        if (String.valueOf(param.get("menuId")).equals("2")) {

            if (CheckNullUtils.isNull(String.valueOf(param.get("twoTwoMenu")))) {
                result.put("code", 6666);
                result.put("data", "fail");
                result.put("msg", "没有接收到twoTwoMenu");
                return result;
            }
            if (CheckNullUtils.isNull(String.valueOf(param.get("twoFirstMenu")))) {
                result.put("code", 6666);
                result.put("data", "fail");
                result.put("msg", "没有接收到twoFirstMenu");
                return result;
            }

            String twoTwoMenu = String.valueOf(param.get("twoTwoMenu"));
            String twoFirstMenu = String.valueOf(param.get("twoFirstMenu"));
            String editTwoId = String.valueOf(param.get("editTwoId"));

            if (String.valueOf(param.get("idEdit")).equals("yes")) {
                int modify = this.permissionService.modify(new Permission() {{
                    setParentId(Long.valueOf(twoFirstMenu));
                    setDescription(twoTwoMenu);
                    setId(Long.valueOf(editTwoId));
                }}, false);

                if (modify != 0) {
                    result.put("code", 10000);
                    result.put("data", "更新成功");
                    result.put("msg", "更新成功~");
                    return result;
                } else {
                    result.put("code", 6666);
                    result.put("data", "更新失败");
                    result.put("msg", "更新失败~");
                    return result;
                }
            }


            add = this.permissionService.add(new Permission() {{
                setParentId(Long.valueOf(twoFirstMenu));
                setDescription(twoTwoMenu);
            }}, false);

        }


        if (String.valueOf(param.get("menuId")).equals("3")) {

            if (CheckNullUtils.isNull(String.valueOf(param.get("threeTwoMenu")))) {
                result.put("code", 6666);
                result.put("data", "fail");
                result.put("msg", "没有接收到threeTwoMenu");
                return result;
            }
            if (CheckNullUtils.isNull(String.valueOf(param.get("threeThreeMenu")))) {
                result.put("code", 6666);
                result.put("data", "fail");
                result.put("msg", "没有接收到threeThreeMenu");
                return result;
            }
            if (CheckNullUtils.isNull(String.valueOf(param.get("url")))) {
                result.put("code", 6666);
                result.put("data", "fail");
                result.put("msg", "没有接收到url");
                return result;
            }
            String url = String.valueOf(param.get("url"));
            String threeTwoMenu = String.valueOf(param.get("threeTwoMenu"));
            String threeThreeMenu = String.valueOf(param.get("threeThreeMenu"));
            String threeId = String.valueOf(param.get("threeId"));
            if (String.valueOf(param.get("idEdit")).equals("yes")) {
                int threeId2 = this.permissionService.modify(new Permission() {{
                    setId(Long.valueOf(threeId));
                    setDescription(threeThreeMenu);
                    setParentId(Long.valueOf(threeTwoMenu));
                    setUrl(url);
                }}, false);
                if (threeId2 == 0) {
                    result.put("code", 6666);
                    result.put("data", "fail");
                    result.put("msg", "更新失败");
                    return result;
                } else {
                    result.put("code", 10000);
                    result.put("data", "更新成功");
                    result.put("msg", "更新成功");
                    return result;
                }
            }

            add = this.permissionService.add(new Permission() {{
                setParentId(Long.valueOf(threeTwoMenu));
                setUrl(url);
                setDescription(threeThreeMenu);
            }}, false);

        }


        if (add != 1) {
            result.put("code", 6666);
            result.put("data", "fail");
            result.put("msg", "保存失败~");
            return result;
        }

        result.put("code", 10000);
        result.put("data", "保存成功~");
        result.put("msg", "保存成功~");
        return result;

    }

    /**
     * 保存父级菜单
     */
    @Override
    public Map<String, Object> saveSmallMenu(Map<String, Object> param) {
        Map<String, Object> result = new HashMap<>();
        if (CheckNullUtils.isNull(String.valueOf(param.get("menuS")))) {
            result.put("code", 6666);
            result.put("data", "fail");
            result.put("msg", "没有接收到父菜单");
            return result;
        }
        if (CheckNullUtils.isNull(String.valueOf(param.get("menuSUrl")))) {
            result.put("code", 6666);
            result.put("data", "fail");
            result.put("msg", "没有接收到url");
            return result;
        }
        if (CheckNullUtils.isNull(String.valueOf(param.get("thisMenu")))) {
            result.put("code", 6666);
            result.put("data", "fail");
            result.put("msg", "没有接收到子菜单");
            return result;
        }

        String menuS = String.valueOf(param.get("menuS"));
        String menuSUrl = String.valueOf(param.get("menuSUrl"));
        String thisMenu = String.valueOf(param.get("thisMenu"));


        List<Permission> permissions = this.permissionService.queryAll(new Permission() {{
            setDescription(menuS);
            setParentId(Long.valueOf("0"));
        }});

        Long id = permissions.get(0).getId();

        int add = this.permissionService.add(new Permission() {{
            setParentId(id);
            setUrl(menuSUrl);
            setDescription(thisMenu);
        }}, false);

        if (add != 1) {
            result.put("code", 6666);
            result.put("data", "fail");
            result.put("msg", "保存失败~");
            return result;
        }

        result.put("code", 10000);
        result.put("data", "保存成功~");
        result.put("msg", "保存成功~");
        return result;
    }

    /**
     * 修改父级名字
     */
    @Override
    public Map<String, Object> updateFName(Map<String, Object> param) {
        Map<String, Object> result = new HashMap<>();
        if (CheckNullUtils.isNull(String.valueOf(String.valueOf(param.get("updateName"))))) {
            result.put("code", 6666);
            result.put("data", "fail");
            result.put("msg", "没有接收到名字");
            return result;
        }
        List<Permission> nameF = this.permissionService.queryAll(new Permission() {{
            setDescription(String.valueOf(param.get("nameF")));
            setParentId(Long.valueOf("0"));
        }});

        Long id = nameF.get(0).getId();

        int updateName = this.permissionService.modify(new Permission() {{
            setId(id);
            setDescription(String.valueOf(param.get("updateName")));
        }}, false);
        if (updateName != 1) {
            result.put("code", 6666);
            result.put("data", "fail");
            result.put("msg", "修改失败~");
            return result;
        }

        result.put("code", 10000);
        result.put("data", "修改成功~");
        result.put("msg", "修改成功~");
        return result;

    }

    /**
     * 删除子菜单
     */
    @Override
    public Map<String, Object> delSName(String id) {
        Map<String, Object> result = new HashMap<>();
        if (CheckNullUtils.isNull(id)) {
            result.put("code", 6666);
            result.put("data", "fail");
            result.put("msg", "没有接收到参数");
            return result;
        }

        int remove = this.permissionService.remove(Long.valueOf(id));

        List<RolePermission> rolePermissions = this.rolePermissionService.queryAll(new RolePermission() {{
            setPermissionId(Long.valueOf(id));
        }});

        for (RolePermission rolePermission : rolePermissions) {
            this.rolePermissionService.remove(rolePermission.getId());
        }

        if (remove != 1) {
            result.put("code", 6666);
            result.put("data", "fail");
            result.put("msg", "删除失败~");
            return result;
        }

        result.put("code", 10000);
        result.put("data", "删除成功~");
        result.put("msg", "删除成功~");
        return result;
    }

    /**
     * 更具id查子菜单
     */
    @Override
    public Map<String, Object> queryZMenuById(String id) {
        Map<String, Object> result = new HashMap<>();
        if (CheckNullUtils.isNull(id)) {
            result.put("code", 6666);
            result.put("data", "fail");
            result.put("msg", "没有接收到参数");
            return result;
        }

        List<Permission> permissions = this.permissionService.queryAll(new Permission() {{
            setId(Long.valueOf(id));
        }});
        result.put("code", 10000);
        result.put("data", permissions);
        result.put("msg", "查询成功~");
        return result;
    }

    /**
     * 根据父id查子菜单
     */
    @Override
    public Map<String, Object> queryZMenuByFId(String id) {
        Map<String, Object> result = new HashMap<>();
        List<Permission> permissions = this.permissionService.queryAll(new Permission() {{
            setParentId(Long.valueOf(id));
        }});
        result.put("code", 10000);
        result.put("data", permissions);
        result.put("msg", "查询成功~");
        return result;

    }

    /**
     * 根据菜单找到对应的btn
     */

    @Override
    public Map<String, Object> findMenusBtns(Map<String, Object> map) {

        Map<String, Object> result = new HashMap<>();

        List<Map<String, Object>> listM = (List<Map<String, Object>>) JSONObject.parse(String.valueOf(map.get("params")));

        HashSet<String> menuS = new HashSet<>();
        for (Map<String, Object> stringObjectMap : listM) {
            String id = (String) stringObjectMap.get("id");
            menuS.add(id);
        }

        ArrayList<Map<String, Object>> btns = new ArrayList<>();


        menuS.forEach(menu -> {
            List<Permission> permissions = this.permissionService.queryAll(new Permission() {{
                setPermissionType(1);
                setId(Long.valueOf(menu));
            }});

            permissions.forEach(permission -> {
                Map<String, Object> singleBtns = new HashMap<>();
                String code = permission.getCode();
                singleBtns.put("name", permission.getDescription());
                singleBtns.put("btns", code);
                btns.add(singleBtns);
            });

        });

        result.put("data", btns);
        result.put("code", 10000);
        result.put("msg", "成功");


        return result;
    }

    /**
     * 赋予角色按钮
     */
    @Override
    @Transactional
    public Map<String, Object> addBtnsShiroToRole(Map<String, Object> map) {

        Map<String, Object> result = new HashMap<>();
        Map<String, Object> params = JSONObject.parseObject(String.valueOf(map.get("params")));

        if (params.size() == 0) {
            result.put("data", "接受的参数没有");
            result.put("code", 10000);
            result.put("msg", "接受的参数没有");
            return result;
        }
        if (CheckNullUtils.isNull(String.valueOf(params.get("name")))) {
            result.put("data", "接受的参数没有name");
            result.put("code", 10000);
            result.put("msg", "接受的参数没有name");
            return result;
        }
        if (CheckNullUtils.isNull(String.valueOf(params.get("btns")))) {
            result.put("data", "接受的参数没有btns");
            result.put("code", 10000);
            result.put("msg", "接受的参数没有btns");
            return result;
        }

        List<String> btnsList = (List<String>) JSONObject.parse(String.valueOf(params.get("btns")));
        String name = String.valueOf(params.get("name"));

        List<Role> roles = this.roleService.queryAll(new Role() {{
            setName(name);
        }});

        Long roleId = roles.get(0).getId();

        List<RolePermission> rolePermissions1 = this.rolePermissionService.queryAll(new RolePermission() {{
            setRoleId(roleId);
        }});
        for (RolePermission rolePermission : rolePermissions1) {

            List<Permission> permissions = this.permissionService.queryAll(new Permission() {{
                setId(rolePermission.getPermissionId());
                setPermissionType(0);
            }});
            if (permissions.size() > 0) {
                this.rolePermissionService.remove(rolePermission.getId());
            }
        }

        int insert = 0;

        for (String s : btnsList) {

            String btns = s.split("_")[1];

            List<Permission> permissions = this.permissionService.queryAll(new Permission() {{
                setDescription(btns);
                setPermissionType(0);
            }});

            Long id = permissions.get(0).getId();

            //保存按钮的时候判断是否存在上级的菜单
            Long parentId = permissions.get(0).getParentId();
            List<RolePermission> rolePermissions = this.rolePermissionService.queryAll(new RolePermission() {{
                setRoleId(roleId);
                setPermissionId(parentId);
            }});
            if (rolePermissions.size() <= 0) {
                this.rolePermissionService.add(new RolePermission() {{
                    setRoleId(roleId);
                    setPermissionId(parentId);
                }}, false);
            }




          /*  List<RolePermission> rolePermissions = this.rolePermissionMapper.selectAll(new RolePermission() {{
                setRoleId(roleId);
                setPermissionId(id);
            }});
            for (RolePermission rolePermission : rolePermissions) {
                this.rolePermissionMapper.deleteByPrimaryKey(rolePermission.getId());
            }*/

            insert = this.rolePermissionService.add(new RolePermission() {{
                setRoleId(roleId);
                setPermissionId(id);
            }}, false);

        }

        result.put("data", "保存成功");
        result.put("code", "10000");
        result.put("msg", "保存成功");
        return result;

    }

    /**
     * 根据角色查找按钮
     */
    @Override
    public Map<String, Object> queryBtnsByRoleId(Map<String, Object> map) {


        Map<String, Object> result = new HashMap<>();
        if (CheckNullUtils.isNull(String.valueOf(map.get("name")))) {
            result.put("data", "没有接受到参数！~");
            result.put("code", "6666");
            result.put("msg", "没有接受到参数！~");
            return result;
        }
        String name = String.valueOf(map.get("name"));


        List<Role> roles = this.roleService.queryAll(new Role() {{
            setName(name);
        }});
        List<RolePermission> rolePermissions = this.rolePermissionService.queryAll(new RolePermission() {{
            setRoleId(roles.get(0).getId());
        }});

        ArrayList<String> strings = new ArrayList<>();
        for (RolePermission rolePermission : rolePermissions) {
            List<Permission> permissions = this.permissionService.queryAll(new Permission() {{
                setId(rolePermission.getPermissionId());
                setPermissionType(0);
            }});
            if (permissions.size() == 0) {
                continue;
            }
            String description = permissions.get(0).getDescription();
            List<Permission> permissions1 = this.permissionService.queryAll(new Permission() {{
                setId(permissions.get(0).getParentId());
                setPermissionType(1);
            }});
            String menuName = permissions1.get(0).getDescription();
            StringBuilder stringBuilder = new StringBuilder();
            StringBuilder append = stringBuilder.append(menuName).append("_").append(description);
            strings.add(append.toString());
        }

        result.put("data", strings);
        result.put("code", 10000);
        result.put("msg", "成共");


        return result;
    }

    /**
     * all in this
     */
    @Override
    public Map<String, Object> allInThis() {

        List<Permission> permissions = this.permissionService.queryAll(new Permission() {{
            setPermissionType(1);
            setParentId(Long.valueOf("0"));
        }});//主菜单

        for (Permission permission : permissions) {
            Map<String, Object> singleFatherMap = new HashMap<>();
            Map<String, Object> oneFatherMap = WebUtils.beanToMap(permission);
            List<Permission> permissions1 = this.permissionService.queryAll(new Permission() {{
                setParentId(permission.getId());
                setPermissionType(1);
            }});//所有的二级菜单
            for (Permission permission1 : permissions1) {
                Map<String, Object> twoMap = WebUtils.beanToMap(permission);
                List<Permission> permissions2 = this.permissionService.queryAll(new Permission() {{
                    setParentId(permission1.getId());
                    setPermissionType(1);
                }});//所有的三级


            }


        }


        return null;
    }
}

